<?php

/**
 * 开发者Domain 类
 * @author: 喵了个咪  <wenzhenxi@vip.qq.com> 2015-07-23
 */
class Domain_Manage {

    /**
     * 验证密码
     */
    public function validationPassWord($oldPw, $uId) {

        //通过Id获取密码
        $Model_Manage = new Model_Manage();
        $rs           = $Model_Manage->getDevelopersById($uId);
        if ($rs['passWord'] != $oldPw) {
            throw new PhalApi_Exception_BadRequest(T('passWord Match'));
        }
    }

    /**
     * 管理员登录
     */
    public function login($username, $password) {

        $Model_Managers = new Model_Manage();
        $adminId        = $Model_Managers->getDevelopersByName($username, $password);
        if (!$adminId) {
            throw new PhalApi_Exception_BadRequest(T('No User'));
        }
        if (!$adminId['type']) {
            throw new PhalApi_Exception_BadRequest(T('No User Type'));
        }
        return $adminId;
    }

    /**
     * 获取账户列表
     */
    public function getManagList() {

        //获取出来所有的账户列表
        $Model_Manage = new Model_Manage();
        $ManagList    = $Model_Manage->getManagList();
        foreach ($ManagList as $k => $v) {
            //通过用户ID获取角色IDlist
            $Model_ManageRole = new Model_ManageRole();
            $rs2              = $Model_ManageRole->getManagIdListByRoleIdList(DI()->base->getIndex($v, 'adminId'));
            $RoleIdlist       = array();
            foreach ($rs2 as $varule) {
                $RoleIdlist[] = DI()->base->getIndex($varule, 'rId');
            }
            //通过角色Idlist获取角色名称
            $Model_Role            = new Model_Role();
            $RoleNameList          = $Model_Role->getRoleIdlistBuRoleName($RoleIdlist);
            $ManagList[$k]['role'] = $RoleNameList;
        }
        return $ManagList;
    }

    /**
     * 验证用户名是否存在
     */
    public function increaseValidation($userName) {

        //通过名称获取用户ID
        $Model_Manage = new Model_Manage();
        $ManagList    = $Model_Manage->getuserNameById($userName);
        if ($ManagList) {
            throw new PhalApi_Exception_BadRequest(T('userName existing'));
        }
    }

    /**
     * 创建用户
     */
    public function increaseManage($userName, $passWord, $name, $phone) {

        //创建用户
        $Model_Manage = new Model_Manage();
        $adminId      = $Model_Manage->increaseManage($userName, $passWord, $name, $phone);
        if (!$adminId) {
            throw new PhalApi_Exception_BadRequest(T('No Manage Increase'));
        }
        return $adminId;
    }

    /**
     * 修改用户
     */
    public function updateManage($adminId, $passWord, $name, $phone) {

        //修改用户
        $Model_Manage = new Model_Manage();
        $rs           = $Model_Manage->updateManage($adminId, $passWord, $name, $phone);
        if ($rs === false) {
            throw new PhalApi_Exception_BadRequest(T('No updateManage'));
        }
    }

    /**
     * 停用/启用帐户
     */
    public function updateManageType($adminId, $type) {

        //修改用户
        $Model_Manage = new Model_Manage();
        $rs           = $Model_Manage->updateManageType($adminId, $type);
        if ($rs === false) {
            throw new PhalApi_Exception_BadRequest(T('No updateManage'));
        }
    }

    /**
     * 修改密码
     */
    public function setPassWord($newPw, $uId) {

        $Model_Manage = new Model_Manage();
        $rs           = $Model_Manage->setPassWord($newPw, $uId);
        if ($rs === false) {
            throw new PhalApi_Exception_BadRequest(T('No setPassWord'));
        }
    }

    /**
     * 修改信息
     */
    public function setInfo($uId, $name, $phone) {

        $Model_Manage = new Model_Manage();
        $rs           = $Model_Manage->updateManage2($uId, $name, $phone);
        if ($rs === false) {
            throw new PhalApi_Exception_BadRequest(T('No setInfo'));
        }
    }

    /**
     * 通过用户ID获取菜单和apiId列表
     */
    public function getadminIdBymListapiList($adminId) {

        //通过Id获取角色
        $Model_ManageRole = new Model_ManageRole();
        $rs               = $Model_ManageRole->getManagIdListByRoleIdList($adminId);
        $RoleIdList       = array();
        foreach ($rs as $v) {
            $RoleIdList[] = $v['rId'];
        }

        //过滤角色
        $Model_Role = new Model_Role();
        $rs         = $Model_Role->filterId($RoleIdList);
        $RoleIdList = array();
        foreach ($rs as $v) {
            $RoleIdList[] = $v['rId'];
        }

        //获取权限ID列表
        $Model_RolePrivilege = new Model_RolePrivilege();
        $rs                  = $Model_RolePrivilege->getpriIdByPrivilegeIdlist($RoleIdList);
        $PrivilegeIdList     = array();
        foreach ($rs as $v) {
            $PrivilegeIdList[] = $v['priId'];
        }
        //过滤权限
        $Model_Privilege = new Model_Privilege();
        $rs              = $Model_Privilege->filterId($PrivilegeIdList);
        $PrivilegeIdList = array();
        foreach ($rs as $v) {
            $PrivilegeIdList[] = $v['priId'];
        }

        //通过权限获得APIListID
        $Model_PrivilegeApi = new Model_PrivilegeApi();
        $rs                 = $Model_PrivilegeApi->getpriIdByapiId($PrivilegeIdList);
        $ApiIdList          = array();
        foreach ($rs as $v) {
            $ApiIdList[] = $v['apiId'];
        }
        //过滤权限Api
        $Model_Api = new Model_Api();
        $rs        = $Model_Api->filterId($ApiIdList);
        $ApiIdList = array();
        foreach ($rs as $v) {
            $ApiIdList[] = $v['apiId'];
        }

        //通过API获取菜单的ID
        $Model_ApiMenu = new Model_ApiMenu();
        $rs            = $Model_ApiMenu->getApiIdByMenuId($ApiIdList);
        $mIdList       = array();
        foreach ($rs as $v) {
            $mIdList[] = $v['mId'];
        }
        //获取菜单列表
        $Model_Menu = new Model_Menu();
        $mList      = $Model_Menu->getmIdListByList($mIdList);
        foreach ($mList as $k => $v) {
            //通过ID 获取项目名称ID
            $Model_ProjectMenu = new Model_ProjectMenu();
            $pId               = $Model_ProjectMenu->getmIdBypId($v['mId']);
            //通过$pId获取项目名
            $Model_Project            = new Model_Project();
            $proname                  = $Model_Project->getIdByNmae($pId['pId']);
            $mList[$k]['projectName'] = $proname['name'];
            $mList[$k]['pId']         = $proname['pId'];
        }
        return array($mList, $ApiIdList);
    }

    /**
     * 验证adminId是否存在
     */
    public function checkadminId($adminId) {

        $Model_Manage = new Model_Manage();
        $admin        = $Model_Manage->getDevelopersById($adminId);
        if (!$admin) {
            throw new PhalApi_Exception_BadRequest(T('No Admin'), 201);
        }
        return;
    }
}